package component;

import com.google.gson.Gson;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.JBColor;
import model.Translation;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.io.IOException;

public class TranslateComponent implements ProjectComponent {



    @Override
    public void projectOpened() {
        //在项目启动得时候调用
    }

    @Override
    public void projectClosed() {
        // 在项目关闭的时候调用
    }

    @NotNull
    @Override
    public String getComponentName() {
        return "translate";
    }

    public void show(AnActionEvent e) {
        final Editor mEditor = e.getData(PlatformDataKeys.EDITOR);
        if (null == mEditor) {
            return;
        }
        // 获取当前选中的单词
        SelectionModel model = mEditor.getSelectionModel();
        final String selectedText = model.getSelectedText();
        if (TextUtils.isEmpty(selectedText)) {
            return;
        }

        String baseUrl = "http://fanyi.youdao.com/openapi.do?keyfrom=Skykai521&key=977124034&type=data&doctype=json&version=1.1&q=";

        OkHttpClient okHttpClient = new OkHttpClient();

        final Request request = new Request.Builder()
                .url(baseUrl + selectedText)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            String string = call.execute().body().string();
            String errorCode = string.substring(string.indexOf("errorCode") + 11, string.indexOf("errorCode") + 12);
            String result = "";
            if (StringUtils.equals(errorCode, "20")) {
                result = "要翻译的文本过长";
            }
            if (StringUtils.equals(errorCode, "30")) {
                result = "无法进行有效的翻译";
            }
            if (StringUtils.equals(errorCode, "40")) {
                result = "不支持的语言类型";
            }
            if (StringUtils.equals(errorCode, "50")) {
                result = "无效的key";
            }
            if (StringUtils.equals(errorCode, "60")) {
                result = "无词典结果，仅在获取词典结果生效";
            }
            if (StringUtils.equals(errorCode, "0")) {
                Translation translation = new Gson().fromJson(string, Translation.class);
                // 展示出来
                if (translation.getBasic() == null) {
                    showPopupBalloon(mEditor, translation.getTranslation().toString(), "");
                    return;
                }
                showPopupBalloon(mEditor, translation.getTranslation().toString(), translation.getBasic().getExplains().toString());
                return;
            }
            showPopupBalloon(mEditor, result, "");
            return;
        } catch (
                IOException e1) {
            showPopupBalloon(mEditor, e1.getMessage(), "");
        }
    }

    private void showPopupBalloon(Editor mEditor, String result, String explanis) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            public void run() {
                JBPopupFactory factory = JBPopupFactory.getInstance();
                factory.createHtmlTextBalloonBuilder(
                        "<!DOCTYPE html>\n" +
                                "<html>\n" +
                                "<head>\n" +
                                "    <meta charset=\"utf-8\">\n" +
                                "</head>\n" +
                                "<body>\n" +
                                "    <div id = \"app\">\n" +
                                "        <div id=\"container\">\n" +
                                "            <div>" + result + "</div>\n" +
                                "        </div>\n" +
                                "        <div id=\"explains\">\n" +
                                "            <div>" + explanis + "</div>\n" +
                                "        </div>\n" +
                                "    </div>\n" +
                                "</body>\n" +
                                "</html>"
                        , null, new JBColor(new Color(186, 238, 186), new Color(73, 117, 73)), null)
                        .setFadeoutTime(5000)
                        .createBalloon()
                        .show(factory.guessBestPopupLocation(mEditor), Balloon.Position.below);
            }
        });
    }
}
